<?php

namespace App\Http\Controllers\Admin;

use App\Activity;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Carbon;

class ActivityController extends Controller
{
    /*
    *
    */
    public function index(Request $request){
        $searchKey=$request->searchKey;
        $searchTime=$request->searchTime;
        $status=$request->status;
        return view('admin.activity.list',compact('searchKey','searchTime','status'));
    }
    public function ajax_list(Request $request){

        $theme=Activity::query();
        if($request->searchKey){
            $theme=$theme->where('title','like', '%'.$request->searchKey.'%');
        }

        if($request->searchTime&&$request->searchTime!=''){

            $timeArray=explode('~',$request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $theme=$theme->whereBetween('created_at',[$start_time,$end_time]);
        }

        if($request->status&&$request->status!=''){

            if($request->status==1){
                $theme=$theme->where('end_time','>',Carbon::now());
            }else{
                $theme=$theme->where('end_time','<',Carbon::now());
            }
        }

        $theme=$theme->paginate($request->limit)
            ->toArray();

        return $this->page_json_render($theme);
    }
    //创建
    public function create(Request $request){
        return view('admin.activity.edit_add');
    }
    //储存
    public function store(Request $request){
        $this->filter($request);
        $title=$request->title;
        $image=$request->image;
        $date=$request->date;
        $timeArray=explode('~',$date);
        $start_time=trim($timeArray[0]);
        $end_time=trim($timeArray[1]);
        $theme=new Activity();
        $theme->title=$title;
        $theme->content=$request->content1;
        $theme->start_time=$start_time;
        $theme->end_time=$end_time;
        $theme->image=$request->image;
        $theme->save();
        return redirect()->route('admin.activity_index')->with(
            $this->success()
        );
    }
    //编辑
    public function edit(Request $request,$id){

        $model=Activity::findOrFail($id);

        return view('admin.activity.edit_add',compact('id','model'));
    }
    public function update(Request $request){
        $id=$request->id;
        $this->filter($request);
        $model=Activity::findOrFail($id);
        $title=$request->title;
        $image=$request->image;
        $date=$request->date;
        $timeArray=explode('~',$date);
        $start_time=trim($timeArray[0]);
        $end_time=trim($timeArray[1]);
        $model->title=$title;
        $model->content=$request->content1;
        $model->start_time=$start_time;
        $model->end_time=$end_time;
        $model->image=$request->image;
        $model->save();
        return redirect()->route('admin.activity_index')->with(
            $this->success()
        );
    }
    public function del(Request $request){
        $id=$request->id;
        $model=Activity::findOrFail($id);
        $model->delete();
        return $this->del_success();

    }
    public function delAll(Request $request){

        Activity::whereIn('id',$request->data)->delete();

        return $this->del_success();

    }
    protected function filter($request){
        $this->validate($request,
            [
                'title'=>'required',
                'date'=>'required'

            ]);
    }
}
